﻿using System;
using System.Collections.Generic;
using System.Web;
using PayRoller.Code;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using System.Configuration;
using System.Web.Profile;
using Huayou.UI.WebControls;
using System.Data;
using HySoft.Cash.BaseClass;
using Huayou.Common;

namespace PayRoller.Manage.OrgInfo
{
    public class OrgList : ParentPage, IRequiresSessionState
    {
        // Fields
        protected Button bnSearch;
        protected Huayou.UI.WebControls.DataGridTable dgdList;
        protected HtmlForm form1;
        protected TextBox txtABN;
        protected TextBox txtOrgName;
        protected TextBox txtParamstr;
        protected TextBox txtSid;

        // Methods
        protected void bnSearch_Click(object sender, EventArgs e)
        {
            this.dgdList.DataBind();
        }

        protected void dgdList_DataBinding(object sender, EventArgs e)
        {
            string commandText = "";
            string str2 = "";
            DataTable dt = new DataTable();
            if (!this.txtOrgName.Text.Trim().Equals(""))
            {
                str2 = str2 + " and OrgName like '%" + this.txtOrgName.Text + "%' ";
            }
            if (!this.txtABN.Text.Trim().Equals(""))
            {
                str2 = str2 + " and ABN like '%" + this.txtABN.Text + "%' ";
            }
            if (!this.txtSid.Text.Trim().Equals(""))
            {
                str2 = str2 + " and Sid = " + this.txtSid.Text;
            }
            if (str2.Length > 4)
            {
                str2 = str2.Substring(4);
            }
            if (str2.Equals(""))
            {
                commandText = "select * from tOrgInfo order by Sid desc";
            }
            else
            {
                commandText = "select * from tOrgInfo where " + str2 + " order by Sid desc";
            }
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["SQLConnString"], CommandType.Text, commandText).Tables[0];
            CommonBasePage.SetPage(this.dgdList, dt);
            base.AddValue("pageno", Convert.ToString((int)(this.dgdList.CurrentPageIndex + 1)));
            this.txtParamstr.Text = base.PackPart(base.ParamValue);
        }

        protected void dgdList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemIndex > -1)
            {
                DataRowView drv= e.Item.DataItem as DataRowView;
                if (drv == null)
                {
                    return;
                }

                if (e.Item.Cells[5].Text.Trim().Equals("1"))
                {
                    e.Item.Cells[5].Text = "Yes";
                }
                else
                {
                    e.Item.Cells[5].Text = "No";
                }

                Literal litInvoiceStatus= e.Item.FindControl("litInvoiceStatus") as Literal;
                if (litInvoiceStatus != null)
                {
                    int currentOrgID = Convert.ToInt32(drv["Sid"]);
                    bool isOverDued = false;
                    for (int i = 0; i < OverDuedInvoiceList.Count; i++)
                    {
                        if (OverDuedInvoiceList[i].OrgSid == currentOrgID)
                        {
                            isOverDued = true;
                            break;
                        }
                    }

                    if (isOverDued == true)
                    {
                        litInvoiceStatus.Text = "OverDue";
                    }
                    else
                    {
                        litInvoiceStatus.Text = "Normal";
                    }
                }
            }
        }

        private List<TinvoicelistDT> overDuedInvoiceList;
        
        private List<TinvoicelistDT> OverDuedInvoiceList
        {
            get
            {
                if (overDuedInvoiceList == null)
                {
                    overDuedInvoiceList = TinvoicelistBN.GetOverDueInvoiceList();
                }
                return overDuedInvoiceList;
            }
        }

        protected void dgdList_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.dgdList.CurrentPageIndex = e.NewPageIndex;
            this.dgdList.DataBind();
        }

        protected override void OnInit(EventArgs e)
        {
            base.m_ModuleCode = "";
            base.CheckLogin(true);
            base.CheckPerm();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                base.ParamValue = base.UnPack();
                this.txtParamstr.Text = base.PackPart(base.ParamValue);
                string str = base.GetValue("pageno");
                if (str != null)
                {
                    this.dgdList.CurrentPageIndex = Convert.ToInt32(str) - 1;
                }
                this.dgdList.DataBind();
            }
            else
            {
                base.ParamValue = base.UnPackPart(this.txtParamstr.Text);
            }
        }

        // Properties
        protected HttpApplication ApplicationInstance
        {
            get
            {
                return this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }
    }

}
